//
//  MechanismListViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/11/20.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "MechanismNewInListViewModel.h"

#import "MechanismInfosData.h"

@interface MechanismNewInListViewModel()

@property (nonatomic, readwrite, strong) RACCommand* requestDataCmd;

@property (nonatomic, readwrite, strong) RACCommand* selectCmd;
@property (nonatomic, readwrite, strong) RACCommand* showDetailCmd;

@end

@implementation MechanismNewInListViewModel

- (void)initialize
{
    [super initialize];
    
    @weakify(self);
    
    self.cellHeight = 88.0f;
    
    self.lastPage = 100;
    self.shouldMultiSections = NO;
    /// viewDidLoad后加载数据
    self.shouldRequestRemoteDataOnViewDidLoad = YES;
    /// 允许下拉刷新
    self.shouldPullDownToRefresh = NO;
    /// 允许上拉加载
    self.shouldPullUpToLoadMore = NO;
    
    /// 选中
    //    self.selectCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(ArticleItemViewModel * itemViewModel) {
    //        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    //            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0), dispatch_get_main_queue(), ^{
    //                [subscriber sendNext:itemViewModel];
    //                [subscriber sendCompleted];
    //            });
    //            return nil;
    //        }];
    //    }];
    /// 允许并行执行
    self.selectCmd.allowsConcurrentExecution = NO;
    
    
    self.showDetailCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input)
    {
        @strongify(self);
          
        NSIndexPath* indexPath = input;
        
        NewInMechanismCellModel* vm = self.dataSource[indexPath.row];
        
        MechanismInfoData* infoData = vm.data;
//        SSPPublishContentItemViewModel* vm = self.dataSource[indexPath.section][0];
//        SSPListData* listData = vm.data;
//
//        return [self requestCircleDetailSignal:listData.circleID];
        
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
        {
            [subscriber sendNext:infoData.userID];
            [subscriber sendCompleted];
            
            return nil;
        }];
    }];
}

- (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page
{
    @weakify(self)
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        @strongify(self);
        
        @weakify(self);
        return [[AFNetWorkUtils racPOSTWthURL:url_interface_institution_newadd params:interface_param_institution_newadd(@(page+1), @(List_PageSize))] subscribeNext:^(id x)
                {
                    @strongify(self)
                    
                    /// 转化数据
                    @weakify(self);
                    
                    MechanismInfosData* infosData = [MechanismInfosData modelWithJSON:x];
                    
                    NSArray *viewModels = [infosData.datas.rac_sequence map:^NewInMechanismCellModel *(MechanismInfoData* data)
                                           {
                                               @strongify(self);
                                               
                                               NewInMechanismCellModel* cellModel = [[NewInMechanismCellModel alloc] initWithData:data cellHeight:self.cellHeight];
                                               
                                               return cellModel;
                                           }].array;
                    
                    if (viewModels == nil || viewModels.count < List_PageSize)
                    {
                        self.lastPage = self.page;
                        
                        if (viewModels == nil)
                            viewModels = @[];
                    }
                    else
                    {
                        self.lastPage = 999;
                    }
                    
                    if (self.shouldPullUpToLoadMore)
                    {
                        if (0 == page)
                        {
                            self.dataSource = viewModels;
                        }
                        else
                        {
                            if (self.dataSource == nil)
                            {
                                self.dataSource = viewModels;
                            }
                            else
                            {
                                self.dataSource = [self.dataSource arrayByAddingObjectsFromArray:viewModels];
                            }
                        }
                    }
                    else
                    {
                        self.dataSource = viewModels;
                    }
                    
                    
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                }   error:^(NSError *error) {
                    //错误处理
                    
                    [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:error.userInfo[customErrorInfoKey]}]];
                    [subscriber sendCompleted];
                }
                ];
        
        return nil;
    }];
}

@end
